package com.enqualcomm.kids.activities;

import android.text.InputFilter;
import android.widget.EditText;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.config.userdefault.UserTerminalDefault;
import com.enqualcomm.kids.mvp.terminalbill.BillPresenter;
import com.enqualcomm.kids.mvp.terminalbill.ISetCheckBillView;
import com.enqualcomm.kids.network.socket.response.GetQuerySIMInfoWayResult;
import com.enqualcomm.kids.network.socket.response.QueryUserTerminalInfoResult;
import com.enqualcomm.kids.network.socket.response.TerminallistResult;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.Locale;

import common.utils.PromptUtil;

/**
 * Created by Sunzhigang on 2015/11/3.
 */
@EActivity(R.layout.activity_checkbill_setting)
public class SetCheckBillSettingsActivity extends BaseActivity implements ISetCheckBillView {

    TerminallistResult.Terminal terminal;
    UserTerminalDefault userTerminalDefault;
    BillPresenter billPresenter;

    private String smsString_ye;
    private String smsString_ll;
    private String smsStringnum_h;
    private String smsStringnum_l;

    @ViewById(R.id.checkbill_h_sms_edt)
    EditText checkbill_h_sms_edt;
    @ViewById(R.id.checkbill_l_sms_edt)
    EditText checkbill_l_sms_edt;
    @ViewById(R.id.mobile_operators_h_num_edt)
    EditText mobile_operators_h_num_edt;
    @ViewById(R.id.mobile_operators_l_num_edt)
    EditText mobile_operators_l_num_edt;
    @ViewById(R.id.watch_phone_num_tv)
    TextView watch_phone_num_tv;
    @ViewById(R.id.watch_phone_l_num_tv)
    TextView watch_phone_l_num_tv;

    @ViewById(R.id.notice_three_tv)
    TextView notice_three_tv;

    @ViewById(R.id.describe_tv_2)
    TextView describe_tv_2;

    @Click({R.id.title_bar_title_tv, R.id.title_bar_left_iv})
    void goBackToFront() {
        finish();
    }

    @Click(R.id.submit_btn)
    void setCheckBillSettings() {
        smsString_ye = checkbill_h_sms_edt.getText().toString();
        smsString_ll = checkbill_l_sms_edt.getText().toString();
        smsStringnum_h = mobile_operators_h_num_edt.getText().toString();
        smsStringnum_l = mobile_operators_l_num_edt.getText().toString();
        if (smsString_ye != null && smsString_ll != null
                && smsStringnum_h != null && smsStringnum_h != null
                && !smsString_ye.equals("") && !smsString_ll.equals("")
                && !smsStringnum_h.equals("") && !smsStringnum_l.equals("")) {
            billPresenter.setChecBillSettings();
        } else {
            PromptUtil.toast(this, getString(R.string.set_info_promote));
        }
    }


    @AfterViews
    void setupViews() {

        terminal = getTerminal();
        userTerminalDefault = new UserTerminalDefault(terminal.userterminalid);

        initTitle();
        init();

    }

    private void init() {
        QueryUserTerminalInfoResult.Data terminalInfo = userTerminalDefault.getInfo();

        if ("CN".equals(Locale.getDefault().getCountry().toUpperCase(Locale.getDefault()))) {
            describe_tv_2.setText(getString(R.string.set_check_bill_notice_2));
        } else {
            describe_tv_2.setText(getString(R.string.set_check_bill_notice_oversea_2));
        }
        checkbill_h_sms_edt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        checkbill_l_sms_edt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        mobile_operators_h_num_edt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        mobile_operators_l_num_edt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});

        billPresenter = new BillPresenter(this, terminal.terminalid, null);
        billPresenter.attachSetView(this);
        billPresenter.getCheckBillSettings();


        watch_phone_num_tv.setText(terminalInfo.mobile);
        watch_phone_l_num_tv.setText(terminalInfo.mobile);
        String notice_1 = getString(R.string.set_check_bill_notice_1) + terminalInfo.mobile;
        notice_three_tv.setText(notice_1);
        checkbill_h_sms_edt.setText("");
        checkbill_l_sms_edt.setText("");
        mobile_operators_h_num_edt.setText("");
        mobile_operators_l_num_edt.setText("");
    }

    private void initTitle() {
        QueryUserTerminalInfoResult.Data info = new UserTerminalDefault(terminal.userterminalid).getInfo();
        setupTitleIcon(info, terminal.terminalid, terminal.userterminalid, getString(R.string.b_set_check_bill_title));
        TextView title_bar_terminal_name_tv = (TextView) findViewById(R.id.title_bar_terminal_name_tv);
        title_bar_terminal_name_tv.setText(R.string.b_set_check_bill_title);
    }

    @Override
    public void showCheckBillSettings(GetQuerySIMInfoWayResult result) {
        if (result.result.cxye != null && result.result.cxll != null
                && result.result.number != null && result.result.phone != null && result.result.numberll != null) {
            checkbill_h_sms_edt.setText(result.result.cxye);
            checkbill_l_sms_edt.setText(result.result.cxll);
            if (result.result.cxye != null) {
                checkbill_h_sms_edt.setSelection(result.result.cxye.length());
            }

            mobile_operators_h_num_edt.setText(result.result.number);
            mobile_operators_l_num_edt.setText(result.result.numberll);
            watch_phone_num_tv.setText(result.result.phone);
            watch_phone_l_num_tv.setText(result.result.phone);
            String notice_1 = getString(R.string.set_check_bill_notice_1) + result.result.phone;
            notice_three_tv.setText(notice_1);
        }

    }


    @Override
    public void showToast() {
        PromptUtil.toast(this, getString(R.string.set_check_bill_done));
        finish();
    }

    @Override
    public String getCXYEsms() {
        return smsString_ye != null ? smsString_ye : "";
    }

    @Override
    public String getCXLLsms() {

        return smsString_ll != null ? smsString_ll : "";
    }

    @Override
    public String getHFCXNumber() {

        if (smsStringnum_h != null) {
            return smsStringnum_h != null ? smsStringnum_h : "";
        } else {
            return "";
        }

    }

    @Override
    public String getLLCXNumber() {

        if (smsStringnum_l != null) {
            return smsStringnum_l != null ? smsStringnum_l : "";
        } else {
            return "";
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        billPresenter.dettachSetView();
    }
}
